package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.*;
import com.service.*;
import com.vo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/tempPay")
public class tempController {
    @Resource
    private PayService payService;
    @Resource
    private TempPayService tempPayService;
    @Resource
    private OwnersService ownerService;
    @Resource
    private ItemService itemService;
    @Resource
    private ParkingCarService parkingCarService;
    @Resource
    private HydropowerService hydropowerService;
    @Resource
    private PlotService plotService;
    @Resource
    private ParkService parkService;
    @Resource
    private ParkingLotService parkingLotService;
    @Resource
    private StopcarmoneyService stopcarmoneyService;

    @RequestMapping("/toTempPay")
    public String toTempPay(){
        return "/tollChange/tempPay";
    }
    @RequestMapping("/towaterRecording")
    public String towaterRecording(){
        return "/tollChange/waterRecording";
    }
    @ResponseBody
    @RequestMapping("/selectAllWaterRecording")

    public Result selectAllWaterRecording(Hydropower hydropower){
        PageInfo<Hydropower> pageInfo = tempPayService.selectAllWaterRecording(hydropower);
        return Result.success(pageInfo);
    }
    @ResponseBody
    @RequestMapping("/selectOwnerByPhone")
    public Result selectOwnerByPhone(String phone){
        Owners owners = ownerService.selectOwnerByPhone(phone);
        return Result.success(owners);
    }
    @ResponseBody
    @RequestMapping("/selectItemById")
    public Result selectItemById(Integer id){
        Item item = itemService.selectItemById(id);
        return Result.success(item);
    }
    @ResponseBody
    @RequestMapping("/selectAllEleRecording")
    public Result selectAllEleRecording(HydropowerVo hydropowerVo){
        PageInfo<Hydropower> hydropowers = tempPayService.selectAllEleRecording(hydropowerVo);
        return Result.success(hydropowers);
    }

    @ResponseBody
    @RequestMapping("/selectAllPay")
    public Result selectAllPay(PayVo payVo){
        PageInfo<Pay> payPageInfo = payService.selectAllPay(payVo);
        return Result.success(payPageInfo);
    }
    @ResponseBody
    @RequestMapping("/addPayPi")
    public Result addPayPi(PayVo payVo){
        try {
            Item item = itemService.selectItemById(payVo.getItemid());
            payVo.setPaymoney(item.getOneprice());
            LocalDateTime localDateTime = LocalDateTime.now();
            String createtime = localDateTime.toString();
            payVo.setCreatetime(createtime);
            payVo.setPaystate(0);

            int[] owners = payVo.getOwners();
            for (int i = 0; i < owners.length; i++) {
                payVo.setOwnerid(owners[i]);
                int i1 = payService.addPay(payVo);
            }
            return Result.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("请选中缴费项目");
        }
    }
    @ResponseBody
    @RequestMapping("/selectAllGuDingItem")
    public Result selectAllGuDingItem(){
        List<Item> itemList = itemService.selectAllGuDingItem();
        return Result.success(itemList);
    }
    @RequestMapping("/topayRecording")
    public String topayRecording(){
        return "/pay/payRecording";
    }

    @ResponseBody
    @RequestMapping("/ownerselectPayRecording")
    public Result ownerSelectPayRecording(PayVo payVo){
        PageInfo<Pay> payPageInfo = payService.OwnerSelectPayRecording(payVo);
        return Result.success(payPageInfo);
    }
    @ResponseBody
    @RequestMapping("/selectAllPlot")
    public Result selectAllPlot(){
        List<Plot> plots = plotService.selectPlot();
        return Result.success(plots);
    }
    @ResponseBody
    @RequestMapping("/selectAllPark")
    public Result selectAllPark(){
        List<Park> parks = parkService.selectAllPark();
        return Result.success(parks);
    }
    @ResponseBody
    @RequestMapping("/OwnerSelectStopCar")
    public Result ownerSelectStopCar(StopcarmoneyVo stopcarmoneyVo,String phone){
        System.out.println(stopcarmoneyVo);
        stopcarmoneyVo.setPhone(phone);
        if(stopcarmoneyVo.getParkid()!=null&&stopcarmoneyVo.getParkid()!=0){
            Park park = parkService.selectParkById(stopcarmoneyVo.getParkid());
            stopcarmoneyVo.setParkname(park.getParkcode());
        }
        if(stopcarmoneyVo.getParkinglotid()!=null&&stopcarmoneyVo.getParkinglotid()!=0){
            Parkinglot parkinglot = parkingLotService.selectParkingLotById(stopcarmoneyVo.getParkinglotid());
            stopcarmoneyVo.setParklotname(parkinglot.getParkingcode());
        }
        PageInfo<Stopcarmoney> pageInfo = stopcarmoneyService.OwnerSelectStopCar(stopcarmoneyVo);
        return Result.success(pageInfo);
    }
}
